#include <stdio.h>

/*
队列
    First In First Out.
    判空
        Q.front == Q.rear ? 
        或 size==0? 
        或 tag==0 && front==rear 
        或 (Q.rear+1)%MaxSize==Q.front
    判满
        (Q.front + 1)%MaxSize == Q.rear ?
    出队
        x = Q.data[Q.front];
        Q.front = (Q.front + 1)%Maxsize;
    入队

元素个数
    (rear+Maxsize -rear)%Maxsize
*/


// 静态队列  front标记队头元素  rear标记队尾空位置

#define MaxSize 10

typedef struct{
    int data[MaxSize];
    int front, rear;  // 队头队尾指针

}SqQueue;

void InitQueue(SqQueue &SQ)
{
    SQ.front = 0;
    SQ.rear = 0;
}

bool isEmpty(SqQueue SQ)
{
    return SQ.front == SQ.rear;
}

bool isFull(SqQueue SQ)
{
    return SQ.front == (SQ.rear + 1)%MaxSize;
}

bool EnQueue(SqQueue &SQ, int x)
{
    if (isFull(SQ))
    {
        return false;
    }
    SQ.data[SQ.rear] = x;
    SQ.rear = (SQ.rear + 1)%MaxSize;
    return true;
}

bool DeQueue(SqQueue &SQ, int &x)
{
    if(isEmpty(SQ))
    {
        return false;
    }

    x = SQ.data[SQ.front];
    SQ.front = (SQ.front + 1)%MaxSize;

    return true;
}

bool GetHead(SqQueue SQ, int &x)
{
    if (isEmpty(SQ))
    {
        return false;
    }

    x = SQ.data[SQ.front];
    return true;
}
void show(SqQueue SQ)
{
    puts("show...\n");
    int i = SQ.front;
    while(i != SQ.rear)
    {
        printf("%i ->", SQ.data[i]);
        i=(i+1)%MaxSize;
    }
    puts("");
}

void testSqQueue()
{
    SqQueue q;
    puts("Init...");
    InitQueue(q);
    show(q);

    puts("Enter q...");
    EnQueue(q, 1);
    EnQueue(q, 2);
    EnQueue(q, 3);
    show(q);

    puts("De q....");
    int x;
    DeQueue(q, x);
    printf("%i", x);
    show(q);

    puts("Get head...");
    int h;
    GetHead(q, h);
    printf("%i", h);
    show(q);

    
}

int main()
{
    testSqQueue();
    return 0;
}